﻿--[[
   @brief    ui logic for game_topic_detail ui
   @author   saintzhao
   @version  1.0
   @date     2016-01-07
--]]


game_topic_detail_control = {
    init = function()
        base_utility.log("game_topic_detail_control.init called");
        ierd_tgp.tpf_ui.Require("game_topic_detail\\game_topic_detail_view.lua");
        ierd_tgp.tpf_ui.Require("game_topic_detail\\game_topic_detail_model.lua");
    end;
    
    on_open_game_topic_detail = function(event_id, json_str)
        local info = base_utility.json_to_table(json_str);
        if game_topic_detail_control.is_browser_exist() == false then
            game_topic_detail_view.create_browser();
        end
        
        if info.url ~= nil and info.title ~= nil then
            local url = web_utility.make_login_status_url(info.url,
                login_model.main_uin);
            game_topic_detail_view.show_browser(1, info.title);
            game_topic_detail_view.navigate(url, info.width, info.height, info.have_scroll);
        end;
    end;

    is_browser_exist = function()
      local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
      if component == 0 or component == nil then
        return false;
      end
      return true;
    end;
    
    update_predownload_result = function(event_id, json_param)
        ierd_tgp.tpf_ui.Log("[game_topic_detail]update_predownload_result,param = "..json_param);
        game_topic_detail_control.send_data_to_js("send_preupdate_result_to_preknow", json_param);
    end;
    
    update_predownload_state = function(event_id, json_param)
        ierd_tgp.tpf_ui.Log("[game_topic_detail]update_predownload_state,param = "..json_param);
        local param = base_utility.json_to_table(json_param);
        if param.game_id ~= nil and param.game_id ~= "" then
           game_topic_detail_model.preknow_detail_game_id = param.game_id;
        end;
        if game_topic_detail_model.preknow_detail_game_id ~= 0 then
            local state_info = {};
            local game_state_data = comm_center.call_service("SrvGetGameState", {game_id = game_topic_detail_model.preknow_detail_game_id});
            local temp_list = game_state_data.state;
            if temp_list.iPre_download == 1 then
                local game_lib =
                    base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
                local preupdate_status_info = ierd_tgp.game_library.Pre_Update_Status_Info:new();
                if game_lib ~= nil then
                    game_lib:get_preupdate_status_info(
                        game_topic_detail_model.preknow_detail_game_id, preupdate_status_info);
                end;
                if preupdate_status_info ~= nil and temp_list ~= nil then
                    state_info = {
                        game_id = temp_list.iGame_id,
                        state = temp_list.iState,
                        remian_time = temp_list.iRemain_time,
                        progress = temp_list.iProgress,
                        download_mode = temp_list.download_mode,
                        pre_download = temp_list.iPre_download,
                        pre_download_status = temp_list.pre_download_status,
                        remian_times = preupdate_status_info.formal_update_after_,
                        phase = preupdate_status_info.phase,
                        pre_download_progress = preupdate_status_info.download_progress_,
                        current_status = preupdate_status_info.current_status_,
                        download_speed = preupdate_status_info.download_speed_,
                        pkg_size = preupdate_status_info.pkg_size_,
                        download_time_sum = preupdate_status_info.download_time_sum,
                        versionlist_url = preupdate_status_info.versionlist_url_
                    };
                end;
                if preupdate_status_info ~= nil then
                   preupdate_status_info:delete();
                end;
            else
                state_info = {
                    game_id = temp_list.iGame_id,
                    state = temp_list.iState,
                    remian_time = temp_list.iRemain_time,
                    progress = temp_list.iProgress,
                    download_mode = temp_list.download_mode,
                    pre_download = temp_list.iPre_download,
                    pre_download_status = temp_list.pre_download_status
                };
            end;
            local state_info_json = base_utility.table_to_json(state_info);
            game_topic_detail_control.send_data_to_js("send_preupdate_state_to_preknow",
                state_info_json);
            ierd_tgp.tpf_ui.Log("[game_topic_detail]state_info = ".. state_info_json);
        end;
    end;
    
    save_browser_info = function(event_id, json_param)
        ierd_tgp.tpf_ui.Log("[game_topic_detail]save_browser_info,param = "..json_param);
        local web_info_tbl = base_utility.json_to_table(json_param);
        local count = table.getn(game_topic_detail_model.browser_info);
        local is_exist = false;
        while (count > 0) do
            if web_info_tbl.tab_id == game_topic_detail_model.browser_info[count].tab_id then
                is_exist = true;
            end;
            count = count - 1;
        end;       
        if is_exist == false then
            table.insert(game_topic_detail_model.browser_info, {
                tab_id = web_info_tbl.tab_id;
                web_id = web_info_tbl.web_id;
            });
        end;
        ierd_tgp.tpf_ui.Log("[game_topic_detail]browser_info = "..base_utility.table_to_json(game_topic_detail_model.browser_info));
    end;
    
    send_data_to_js = function(func_name, data)
        local count = table.getn(game_topic_detail_model.browser_info);
        while (count > 0) do
            web_utility.lua_call_js_by_id(func_name, data, 
                game_topic_detail_model.browser_info[count].web_id);
            count = count - 1;
        end;       
    end;
};
game_topic_detail_control.init();